<?php

declare(strict_types=1);
/**
 * 
 */
namespace WanZ\App\User\Action;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Context\Context;
use WanZ\App\User\Logic\UserLogic;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;

/**
 * 登陆验证
 * @Controller
 */
class Login extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var UserLogic
     */
    private $userLogic;

    /**
     * @RequestMapping(path="/auth/login", methods="GET")
     */
    public function handle(): array
    {
        ## 请求参数
        $params = $this->request->inputs(
            ['auth_code', 'login_type', 'mini_source', 'company_id'], 
            ['mini_source' => 'wz']
        );

        ## 类型验证
        $this->validated($params);

        Context::set('minSource', (string) $params['mini_source']);

        return $this->userLogic->login(
            (string) $params['login_type'], 
            (string) $params['auth_code'], 
            (int) $params['company_id']
        );
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'auth_code' => 'required|string|size:32|bail',
            'login_type' => 'required|string|in:ali,wx|bail',
            'mini_source' => 'string|in:wz,wz_not_ad|bail',
            'company_id' => 'required|integer|min:1|bail',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
            'auth_code.required' => 'authCode 必填',
            'auth_code.string' => 'authCode 必需为字符串',
            'auth_code.size' => 'authCode 字符串长度为固定值：32',
            'login_type.required' => '登录类型 必须',
            'login_type.string' => '登录类型 必需为字符串',
            'login_type.in' => '登录类型 非法 值必须在列表内：[wx,ali]',
            'mini_source.string' => '小程序来源 必需为字符串',
            'mini_source.in' => '小程序来源 非法 值必须在列表内：[wz,wz_not_ad]',
            'company_id.required' => '公司ID 必填',
            'company_id.integer' => '公司ID 必需为整数',
            'company_id.min' => '公司ID 值必须大于1',
        ];
    }
}
